package com.jumi.microservice.rocketmq;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.jumi.microservice.common.constant.ImageResizeEnum;
import com.jumi.microservice.common.utils.ImageResizeUtil;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.service.IMessageService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by mazhao
 * @Classname SendMessageHandler
 * @Description TODO
 * @Date 2020/11/5 15:14
 */
@Service
public class SendMessageHandler {
    @Resource
    ProducerBean producerBean;
    @DubboReference
    IMessageService iMessageService;

    private static final Logger log = LoggerFactory.getLogger(SendMessageHandler.class);

    /**
     * 发送消息
     */
    public boolean sendMessage(String orderNo, String appMessageContent, String stationMessageContent,String messageImg, Long receiverMemberId, Integer templateId){
        String template = "{" +
                "  \"appMessage\": {" +
                "    \"messageContent\": \""+appMessageContent+"\"" +
                "  }," +
                "  \"messageStation\": {" +
                "    \"messageContent\": \""+stationMessageContent+"\"" +
                "  }," +
                "  \"link\": {" +
                "    \"orderNo\": \""+orderNo+"\"" +
                "  }," +
                "  \"messageImg\": \""+ ImageResizeUtil.imageResize(messageImg, ImageResizeEnum.LIST)+"\"," +
                "  \"receiverMemberId\": "+receiverMemberId+"," +
                "  \"senderMemberId\": 0," +
                "  \"templateId\": "+templateId+
                "}";

        JSONObject jsonObject = JSONObject.parseObject(template);
        try {
            Message message = new Message("message", "java", "", JSONObject.toJSONBytes(jsonObject));
            producerBean.send(message);
        } catch (Exception e) {
            log.info("推送消息队列发送失败[{}]",e.getMessage());
            return false;
        }
        log.info("推送消息队列发送成功[{}]",JSONObject.toJSONString(jsonObject));
        return true;
    }

    /**
     * 获取所有模板
     * @return
     */
    public Map<Integer, MessageTemplate> getMessageTemplate(){
        List<MessageTemplate> list =  iMessageService.messageTemplateList();
        Map<Integer, MessageTemplate> map = new HashMap<>(list.size());
        for(MessageTemplate messageTemplate:list){
            map.put(messageTemplate.getId().intValue(),messageTemplate);
        }
        return map;
    }

    /**
     * 获取所有模板
     * @return
     */
    public MessageTemplate getMessageTemplate(Integer templateId){
        return iMessageService.messageTemplateOne(templateId.longValue());
    }

}
